package cn.fuxue.hrms.common.utils;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.Map;

/**
 * 简历结构化
 * author：富学
 * description：调用python fast-api的接口
 */


public class ResumeStructured {
    private static String apiUrl = "http://localhost:7070/";

    public static String parser(String filePath) {
        String responseString = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost uploadFile = new HttpPost(apiUrl);
            File file = new File(filePath);

            // 创建multipart/form-data请求体
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName())
                    .build();

            uploadFile.setEntity(reqEntity);
            System.out.println("Executing request: " + uploadFile.getRequestLine());

            // 执行请求并处理响应
            try (CloseableHttpResponse response = httpClient.execute(uploadFile)) {
                System.out.println("Response status: " + response.getStatusLine());
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    responseString = EntityUtils.toString(responseEntity);
                    // 创建Gson实例
                    Gson gson = new Gson();

                    // 将JSON字符串转换为Map
                    Map<String, Object> map = gson.fromJson(responseString, Map.class);
                    System.out.println("Response content: " + responseString);
                    responseString = readJsonFile("D:\\python高薪\\路飞学城\\python\\AI\\ML\\program\\ResumeStructured\\data\\save\\" + map.get("task_id")+".json");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseString;
    }

    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }





}
